package com.lz.stock.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author 廉壮
 * @version 1.0
 */
public class Generate {

    public static void main(String[] args) {
        String url ="jdbc:mysql://localhost:3306/a-db-stock?serverTimezone=GMT%2b8";
        String username ="root";
        String password ="root";
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("lz") // 设置作者
                            .outputDir("C:\\Users\\廉壮\\IdeaProjects\\d20240829\\分布式事务seata\\demo\\service-stock\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.lz") // 设置父包名
                                .moduleName("stock") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "C:\\Users\\廉壮\\IdeaProjects\\d20240829\\分布式事务seata\\demo\\service-stock\\src\\main\\resources\\mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude() // 设置需要生成的表名
                                .addTablePrefix() // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
